//
//  CLTabBar.m
//  CLBrowser
//
//  Created by chuck on 2021/7/8.
//

#import "CLTabBar.h"
#import <YYKit/UIView+YYAdd.h>
@interface CLTabBar()
/// divider
@property (nonatomic, readwrite, weak) UIView *divider;
@property (nonatomic, readwrite, strong) UIButton *btnCenter;
@property (nonatomic, assign) CGSize oldSize;
@property (nonatomic, assign) CGFloat oldY;

@end
@implementation CLTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        /// 去掉tabBar的分割线,以及背景图片
        //[self setShadowImage:[UIImage new]];
        //[self setBackgroundImage:[UIImage hi_resizableImage:@"tabbarBkg_5x49"]];
        
        /// 添加细线,
//        UIView *divider = [[UIView alloc] init];
//        if (@available(iOS 13.0, *)) {
//            divider.backgroundColor = UIColor.separatorColor;
//            self.backgroundColor = UIColor.systemBackgroundColor;
//        } else {
//            self.backgroundColor = [UIColor whiteColor];
//            divider.backgroundColor = CLColor(167.0f, 167.0f, 170.0f);
//        }
//        self.layer.borderColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.0].CGColor;
        self.tintColor = HEXRGB(0x333333);
//        [self addSubview:divider];
//        self.divider = divider;
//        self.layer.borderWidth = 0.5;
    }
    return self;
}

#pragma mark - 布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self bringSubviewToFront:self.divider];
    self.divider.height = CLGlobalBottomLineHeight;
    self.divider.width = SCREENWIDTH;
    
    
    if (CGSizeEqualToSize(self.oldSize, CGSizeZero)) {
        NSInteger i = 0;
        for (UIView * tabbarBtn in self.subviews) {
            if ([tabbarBtn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                self.oldSize =tabbarBtn.frame.size;
                self.oldY = tabbarBtn.frame.origin.y;
                if (i==2) {
                    self.ivRedDot.origin = CGPointMake(CGRectGetMaxX(tabbarBtn.frame) -tabbarBtn.frame.size.width/2 + 10, 7);
                }
                i++;
            }
        }
    }else{
        
        for (UIView * tabbarBtn in self.subviews) {
            if ([tabbarBtn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                tabbarBtn.size = self.oldSize;
                tabbarBtn.top = self.oldY;
            }
        }
    }
    
    self.btnCenter = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnCenter setImage:[UIImage imageNamed:@"home_search"] forState:0];
    [self.btnCenter addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    self.btnCenter.frame = CGRectMake((SCREENWIDTH - 60) / 2, -22, 70, 70);
    [self addSubview:self.btnCenter];
}

- (void)clickAction:(UIButton *)sender {
    !self.centerBtnClick ?: self.centerBtnClick();
}

- (UIImageView *)ivRedDot {
    if (!_ivRedDot) {
        _ivRedDot = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 6, 6)];
        _ivRedDot.backgroundColor = [UIColor redColor];
        _ivRedDot.layer.cornerRadius = 3;
        _ivRedDot.layer.masksToBounds = YES;
        _ivRedDot.hidden = YES;
        [self addSubview:_ivRedDot];
    }
    [self bringSubviewToFront:_ivRedDot];
    return _ivRedDot;
}
@end
